package com.person.jc.service.base.standard;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.SQLSpell;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BdStandardMatterService extends Service {
	
	public final static BdStandardMatterService me = new BdStandardMatterService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select matter_id,matter_name,matter_code, measure_scope,measure_unit,memo,cert_code,device_type,device_id_,order_no,date_format(end_date, '%Y-%m-%d') end_date,datediff(end_date , now()) diff_day, remark," +
				"("+SQLSpell.kindName("device_type", "t.device_type")+") device_type_name, " +
				"("+SQLSpell.deviceName("t.device_id_")+") device_id_name "+ 
				" from bd_standard_matter t where if_valid = 1");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("matterName")))){
			sql.append(" and matter_name like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("certCode")))){
			sql.append(" and cert_code like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("matterId")))){
			sql.append(" and matter_id = '"+ temp +"'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceType")))){
			sql.append(" and device_type = '"+ temp +"'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("standardMatterIds")))){
			sql.append(" and matter_id in ("+ temp +")");

		}
		sql.append(" order by device_type, order_no, diff_day , matter_name");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		Record matter = new Record();
		matter.set("matter_name", map.get("matterName"));
		matter.set("matter_code",map.get("matterCode"));
		matter.set("measure_scope", map.get("measureScope"));
		matter.set("memo", map.get("memo"));
		matter.set("cert_code", map.get("certCode"));
		matter.set("end_date", map.get("endDate"));
		matter.set("device_type", map.get("deviceType"));
		matter.set("remark", map.get("remark"));
		matter.set("measure_unit",map.get("measureUnit"));
		Object orderNo = Utils.isNotEmpty(String.valueOf(map.get("orderNo")))?map.get("orderNo"):null;
		Object deviceId = Utils.isNotEmpty(String.valueOf(map.get("deviceId")))?map.get("deviceId"):null;
		matter.set("order_no", orderNo);
		matter.set("device_id_", deviceId);
		Db.save("bd_standard_matter", matter);
		
		super.setCRUD("标准物质管理", "C", "新增标准物质："+map.get("matterName"));
	}
	
	public void update(Map<String,Object> map){
		Record matter = new Record();
		matter.set("matter_id", map.get("matterId"));
		matter.set("matter_name", map.get("matterName"));
		matter.set("matter_code",map.get("matterCode"));
		matter.set("measure_scope", map.get("measureScope"));
		matter.set("memo", map.get("memo"));
		matter.set("cert_code", map.get("certCode"));
		matter.set("end_date", map.get("endDate"));
		matter.set("device_type", map.get("deviceType"));
		matter.set("remark", map.get("remark"));
		matter.set("measure_unit",map.get("measureUnit"));
		Object orderNo = Utils.isNotEmpty(String.valueOf(map.get("orderNo")))?map.get("orderNo"):null;
		Object deviceId = Utils.isNotEmpty(String.valueOf(map.get("deviceId")))?map.get("deviceId"):null;
		matter.set("order_no", orderNo);
		matter.set("device_id_", deviceId);
		Db.update("bd_standard_matter", "matter_id", matter);
		super.setCRUD("标准物质管理", "U", "修改标准物质，编号："+map.get("matterId"));
	}
	
	public void delete(String matterId){
		String sql = " update bd_standard_matter set if_valid = 0 where matter_id = ? ";
		Db.update(sql, matterId);
		super.setCRUD("标准物质管理", "D", "删除标准物质，编号："+matterId);
	}
	
	public long checkName(String matterName, String matterId){
		String sql = "select count(1) from bd_standard_matter where matter_name = ? and if_valid <> 0 ";
		if(Utils.isNotEmpty(matterId)){
			sql += " and matter_id <> " + matterId;
		}
		return Db.queryLong(sql, matterName);
	}

	public long checkMatterCode(String matterCode, String matterId) {
		String sql = "select count(1) from bd_standard_matter where matter_code = ? and if_valid <> 0 ";
		if(Utils.isNotEmpty(matterId)){
			sql += " and matter_id <> " + matterId;
		}
		return Db.queryLong(sql, matterCode);
	}

	public long checkCertCode(String certCode, String matterId) {
		String sql = "select count(1) from bd_standard_matter where cert_code = ? and if_valid <> 0 ";
		if(Utils.isNotEmpty(matterId)){
			sql += " and matter_id <> " + matterId;
		}
		return Db.queryLong(sql, certCode);
	}
	
}
